import React from 'react';

import { connect } from 'react-redux';

class ComA extends React.Component {

  handleClickAdd = () => {
    console.log('ComA', this.props);
    this.props.addAction();
  }

  handleClickMinus = () => {
    console.log('ComA', this.props);
    this.props.minusAction();
  }

  render() {
    return (
      <>
        <button style={{marginRight: 10}} onClick={this.handleClickAdd}>点击++</button>
        <button style={{marginRight: 10}} onClick={this.handleClickMinus}>点击--</button>
        <button style={{marginRight: 10}} onClick={this.props.clearAction}>重置</button>
      </>
    )
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    dispatch,
    addAction: () => {
      // 在 dispatch 中传递 action
      dispatch({
        type: 'add_action',
      })
    },
    minusAction: () => {
      // 在 dispatch 中传递 action
      dispatch({
        type: 'minus_action',
      })
    },
    clearAction() {
      dispatch({
        type: 'clear_action',
      })
    }
  }
}

const mapStateToProps = state => ({count: state.count});

// 发送数据，需要传入第二个参数
export default connect(mapStateToProps, mapDispatchToProps)(ComA);